package com.fun_ordering.service.impl;

import com.fun_ordering.service.FoTableService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.fun_ordering.components.PageResult;
import com.fun_ordering.mapper.FoTableMapper;
import com.fun_ordering.pojo.FoTable;
import com.fun_ordering.pojo.FoTableExample;
import com.fun_ordering.pojo.FoTableExample.Criteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 餐桌表服务实现层
 * @author XiaoPi
 *
 */
@Service
public class FoTableServiceImpl implements FoTableService {

	@Autowired
	private FoTableMapper foTableMapper;
	
	/**
	 * 查询全部
	 */
	@Override
	public List<FoTable> findAll() {
		return foTableMapper.selectByExample(null);
	}

	/**
	 * 按分页查询
	 */
	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);		
		Page<FoTable> page=   (Page<FoTable>) foTableMapper.selectByExample(null);
		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 增加
	 */
	@Override
	public void add(FoTable foTable) {
		foTableMapper.insert(foTable);		
	}

	
	/**
	 * 修改
	 */
	@Override
	public void update(FoTable foTable){
		foTableMapper.updateByPrimaryKey(foTable);
	}	
	
	/**
	 * 根据ID获取实体
	 * @param tableId
	 * @return
	 */
	@Override
	public FoTable findOne(Integer tableId){
		return foTableMapper.selectByPrimaryKey(tableId);
	}

	/**
	 * 批量删除
	 */
	@Override
	public void delete(Integer[] tableIds) {
		for(Integer tableId:tableIds){
			foTableMapper.deleteByPrimaryKey(tableId);
		}		
	}
	
	
		@Override
	public PageResult findPage(FoTable foTable, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		
		FoTableExample example=new FoTableExample();
		Criteria criteria = example.createCriteria();
		
		if(foTable!=null){			
						if(foTable.getTableName()!=null && foTable.getTableName().length()>0){
				criteria.andTableNameLike("%"+foTable.getTableName()+"%");
			}	
		}
		
		Page<FoTable> page= (Page<FoTable>)foTableMapper.selectByExample(example);
		return new PageResult(page.getTotal(), page.getResult());
	}
	
}
